/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logical.allhit.gui.action;

import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import javax.swing.Action;
import javax.swing.event.SwingPropertyChangeSupport;

/**
 *
 * @author Sanjeet Jha
 */
public abstract class AbstractAction implements Action {

    private Map<String, Object> map = new HashMap<String, Object>();
    private boolean enabled = true;
    private SwingPropertyChangeSupport changeSupport;
    ResourceBundle bundle =
            ResourceBundle.getBundle("com/logical/allhit/resouer/menu");



    @Override
    public Object getValue(String key) {
        if (key.equals("enabled")) {
            return enabled;
        } else if (map == null) {
            return null;
        } else {
            return map.get(key);
        }
    }

    @Override
    public void putValue(String key, Object newValue) {
        Object oldValue = null;
        if (key.equals("enabled")) {
            if (newValue == null || !(newValue instanceof Boolean)) {
                newValue = true;
            }
            oldValue = enabled;
            enabled = (Boolean) newValue;
        } else {
            if (map == null) {
                map = new HashMap<String, Object>();
            }
            if (map.containsKey(key)) {
                oldValue = map.get(key);
            }
            if (newValue == null) {
                map.remove(key);
            } else {
                map.put(key, newValue);

            }
        }
        firePropertyChange(key, oldValue, newValue);
    }

    @Override
    public void setEnabled(boolean b) {
        boolean oldValue = this.enabled;
        if (b != oldValue) {
            this.enabled = b;
            firePropertyChange("enabled",
                    Boolean.valueOf(oldValue), Boolean.valueOf(b));
        }
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    @Override
    public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
        if (changeSupport == null) {
            changeSupport = new SwingPropertyChangeSupport(this);
        }
        changeSupport.addPropertyChangeListener(listener);
    }

    @Override
    public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
        if (changeSupport != null) {
            return;
        }
        changeSupport.removePropertyChangeListener(listener);
    }

    @Override
    public abstract void actionPerformed(ActionEvent e);

    private void firePropertyChange(String propertyName, Object oldValue,
            Object newValue) {
        if (changeSupport == null
                || (oldValue != null && newValue != null && oldValue.equals(newValue))) {
            return;
        }
        changeSupport.firePropertyChange(propertyName, oldValue, newValue);
    }
}
